<?php
namespace app\setup\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Index extends Controller
{
    public function index()
    {
        //查询
        $id = Session::get('id');
        $list = Db::table('user_info')->where('uid',$id)->find();
        $pub = Db::table('user_publicity')->where('uid',$id)->find();
        $a = Session::get('name');
        var_dump($id);
        var_dump($list);
        $this->assign('list',$list);
        $this->assign('pname',$list['pname']);
        $this->assign('uname',$list['uname']);
        ($list['birth_year'] == null) ? $year = '0000' : $year = $list['birth_year'];
        ($list['birth_mouth'] == null) ? $mouth = '00' : $mouth = $list['birth_mouth'];
        ($list['birth_day'] == null) ? $day = '00' : $day = $list['birth_day'];
        $this->assign('year',$year);
        $this->assign('mouth',$mouth);
        $this->assign('day',$day);

        //判断真实姓名是否公开
        $pub['uname_open'] == 0 ? $uname_open1 = 'selected': $uname_open1 = '';
        $pub['uname_open'] == 1 ? $uname_open2 = 'selected': $uname_open2 = '';
        $pub['uname_open'] == 2 ? $uname_open3 = 'selected': $uname_open3 = '';

        //判断性别是否公开
        $pub['sex_open'] == 0 ? $sex_open1 = 'selected': $sex_open1 = '';
        $pub['sex_open'] == 1 ? $sex_open2 = 'selected': $sex_open2 = '';
        $pub['sex_open'] == 2 ? $sex_open3 = 'selected': $sex_open3 = '';

        //判断生日是否公开
        $pub['birth_open'] == 0 ? $birth_open1 = 'selected': $birth_open1 = '';
        $pub['birth_open'] == 1 ? $birth_open2 = 'selected': $birth_open2 = '';
        $pub['birth_open'] == 2 ? $birth_open3 = 'selected': $birth_open3 = '';

        $this->assign('uname_open1',$uname_open1);
        $this->assign('uname_open2',$uname_open2);
        $this->assign('uname_open3',$uname_open3);

        $this->assign('sex_open1',$sex_open1);
        $this->assign('sex_open2',$sex_open2);
        $this->assign('sex_open3',$sex_open3);

        $this->assign('birth_open1',$birth_open1);
        $this->assign('birth_open2',$birth_open2);
        $this->assign('birth_open3',$birth_open3);

        // $this->assign('mouth',$list['birth_mouth']);
        // $this->assign('day',$list['birth_day']);
        if($list['sex'] == 0){
            $checked1 = 'checked';
        }else{
            $checked1 = '';
        }
        if($list['sex'] == 1){
            $checked2 = 'checked';
        }else{
            $checked2 = '';
        }
        if($list['sex'] == 2){
            $checked3 = 'checked';
        }else{
            $checked3 = '';
        }
        $this->assign('sex',$list['sex']);
        $this->assign('checked1',$checked1);
        $this->assign('checked2',$checked2);
        $this->assign('checked3',$checked3);

        return $this->fetch('index/personaldata');
    }

    //更新数据
    public function update()
    {
        if(request()->isPost()){
            //获取提交的数据
        $data = Request::instance()->post();
        // $pname = Request::instance()->post('pname');
        // $uname = Request::instance()->post('uname');
        // $pname = Request::instance()->post('pname');
        // $pname = Request::instance()->post('pname');
        $id = Session::get('id');
        var_dump($data);
        }
        
        $result = Db::table('user_info')->where('uid',$id)->update(['pname'=>$data['pname'],'uname'=>$data['uname'],'sex'=>$data['sex'],'birth_year'=>$data['year'],'birth_mouth'=>$data['mouth'],'birth_day'=>$data['day']]);
        $pub = Db::table('user_publicity')->where('uid',$id)->update(['uname_open'=>$data['uname_open'],'sex_open'=>$data['sex_open'],'birth_open'=>$data['birth_open']]);
        var_dump($result);
        var_dump($pub);
        // return $this->redirect('index/index');
    }
}
